import { IAction } from "./actions";
import * as Types from "./types";

// 定义一个最小的宽度,如果屏幕宽度小于这个值collapsed的默认值为false
export const WINDOW_WIDTH = 1000;

export interface IDefaultState {
  menu: Array<IReducerMenu>;
  showLoginModal: boolean;
  collapsed: boolean;
  userInfo: null | IReducerUserInfo;
  loading_userInfo: boolean;
  loading_menu: boolean;
}

export interface IReducerMenu {
  icon: string;
  key: string;
  label: string;
  url: string;
}

export interface IReducerUserInfo {
  username: string;
}

export const defaultState: IDefaultState = {
  menu: [],
  // 显示登录弹框
  showLoginModal: false,
  // 左侧菜单是否折叠
  collapsed: window.innerWidth >= WINDOW_WIDTH ? false : true,
  // 当前登录用户的信息
  userInfo: null,
  loading_userInfo: false,
  loading_menu: false
};

export default (state = defaultState, action: IAction): typeof defaultState => {
  switch (action.type) {
    case Types.GET_MENU: {
      return { ...state, menu: action.data };
    }
    case Types.SET_LOGIN_MODEL_VISIBLE: {
      return { ...state, showLoginModal: action.data };
    }
    case Types.SET_COLLAPSED: {
      return { ...state, collapsed: action.data };
    }
    case Types.SET_LOADING_USER_INFO: {
      return { ...state, loading_userInfo: action.data };
    }
    case Types.SET_USER_INFO: {
      return { ...state, userInfo: action.data };
    }
    case Types.SET_LOADING_MENU: {
      return { ...state, loading_menu: action.data };
    }
    default: {
      return state;
    }
  }
};
